package com.cea.papyrustest.uml.deletecommand;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.uml2.uml.Usage;

import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.commands.UMLElementDeleteCommand;

/**
 * Test class for {@link Usage} creation command
 * @author Remi Schnekenburger
 */
public class UsageDeleteCommandTest extends PapyrusCommandTest{

	/**
	 * Default constructor
	 * @param name the name oif the test case
	 */
	public UsageDeleteCommandTest(String name) {
		super(name);	
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		papyrusCommand= new UMLElementCreateCommand(org.eclipse.uml2.uml.Usage.class, model);
		stack.execute(papyrusCommand);
		Usage usage = (Usage)((UMLElementCreateCommand)papyrusCommand).getElement();
		assertNotNull("Usage is null", usage);
		
		papyrusCommand = new UMLElementDeleteCommand(usage, model);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrustest.PapyrusCommandTest#testExecute()
	 */
	@Override
	public void testExecute() {
		stack.execute(papyrusCommand);
		List usages =  getUsages(model);
		assertNotNull("the usage is not deleted", usages);
		assertEquals("There should be no usages in this list", 0, usages.size());
		
		// undo
		stack.undo();
		usages = getUsages(model);
		assertNotNull(usages);
		assertEquals("There should be one usage in this list", 1, usages.size());
		assertTrue("this is not a usage", (usages.get(0) instanceof Usage));
		
		// redo
		stack.redo();
		usages =  getUsages(model);
		assertNotNull("the usage is not deleted", usages);
		assertEquals("There should be no usages in this list", 0, usages.size());
	}
	
	/**
	 * Returns all direct owned usages for this package
	 * @param package_ the parent package tested
	 * @return the list of direct owned usages
	 */
	private List getUsages(org.eclipse.uml2.uml.Package package_) {
		List usages = new ArrayList();
		Iterator it = package_.eContents().iterator();
		while(it.hasNext()) {
			Object element = it.next();
			if(element instanceof Usage) {
				usages.add(element);
			}
		}
		return usages;
	}
}